<?php
	require 'config.php';
	require 'init.php';
	/**/
	function cleanStr($strArray){
		if(is_array($strArray)){
			foreach ($strArray as &$val) {
				$val = str_replace("'",'',$val);
				$val = trim($val);
			}
		} else {
			$strArray = trim($strArray);
			$strArray = str_replace("'",'',$strArray);
		}
		
		return $strArray;
	}
	/**/
	function converttoArray($str){
		$matches = array();
		foreach ($str as $s) {
			$match = explode(',',$s);
			$matches[cleanStr($match[2])] = cleanStr($match);
		}
		
		return $matches;
	}
	/**/
	function getInfoMatch($infoMatchString){
		$infoMatchString = str_replace(" ",'',$infoMatchString);
		$infoMatchString = str_replace("'",'',$infoMatchString);
		$infoMatchString = str_replace(");",'',$infoMatchString);
		$infoMatchString = str_replace("bettingESInfo(",'',$infoMatchString);
		return explode(',',$infoMatchString);
	}
	// init category matche
	$cates = CTable('category_match')->select_all();
	$cateMatch = array();
	foreach ($cates as $cate) {
		$cateMatch[$cate['name']] = $cate['id'];
	}
	//log
	$log = new CLogData();
	$log->addlog(date('H:i:s Y/m/d').'update rate','update.log');
	// init match
	$preMactches = CTable('match')->select_all(array('match_end'=>0));
	$matchAdded = array();
	foreach ($preMactches as $pm) {
		$matchAdded[$pm['code']] = $pm['id'];
	}
	// phế
	$bet_commission = CPortal::settings('bet_commission');
	//get data win29
	$win29 = file_get_contents('http://win29.com/betting.es.php');
	preg_match_all("/addESC\((.*)\);/", $win29, $out);
	$matches = converttoArray($out[1]);	
	/*every match*/
	foreach ($matches as $match) {
		// add catgegory
		if(!isset($cateMatch[$match[1]])){
			$id = CTable('category_match')->insert(array(
				'name' => $match[1]
			));
			$cateMatch[$match[1]] =$id;
		}
		
		//add matches
		if(!isset($matchAdded[$match[2]])){
//		if(true){
			$id = CTable('match')->insert(array(
				'cate_id' => $cateMatch[$match[1]],
				'code' => $match[2],
				'team1' => $match[4],
				'team2' => $match[5],
				'match_start' => $match[3],
				'public' => 0
			));
			
			$matchAdded[trim($match[2])] =$id;
			//echo 'http://www.win29.com/betting.es.php?page=info&matchID='.$match[2].'<br>';
			// add rate
			$infoMatch = file_get_contents('http://www.win29.com/betting.es.php?page=info&matchID='.$match[2]);
			$infoMatch = getInfoMatch($infoMatch);
			
			//-----------------------------
			//http://www.flashscore.com/x/feed/d_od_8QbkWRd8_en_1_eu
			$bet365 = new Bet('http://www.flashscore.com/x/feed/d_od_'.$match[2].'_en_1_eu');
			
			if ($bet365->continue == false) continue;
			$odds = $bet365->getBetODDS();
			$ov_ud = $bet365->getBetOverUnder();
			$hadicap = $bet365->getBetHandicap();
			
			
			//-----------------------------
			$insert = array(
				'match_id' => $id,
				'ft_home' => $bet365->realRate($odds['odds']['home'], $bet_commission),
				'ft_draw' =>$bet365->realRate($odds['odds']['draw'], $bet_commission),
				'ft_away' => $bet365->realRate($odds['odds']['away'], $bet_commission),
				'dc_hod' => $infoMatch[8],
				'dc_aod' => $infoMatch[9],
				'dc_hoa' => $infoMatch[10],
				'ah_home' => $bet365->realRate($hadicap['home'], $bet_commission),
				'ah_away' => $bet365->realRate($hadicap['home'], $bet_commission),
				'ah_rate_goal' => $hadicap['rate'],
				'ahs_home' => $infoMatch[21],
				'ahs_away' => $infoMatch[22],
				'ahs_rate_goal' => $infoMatch[20],
				'ou_over' => $bet365->realRate($ov_ud['over'], $bet_commission),
				'ou_rate_goal' => $ov_ud['total'],
				'ou_under' => $bet365->realRate($ov_ud['under'], $bet_commission),
				'ts_even' => 2-$bet_commission,
				'ts_odd' => 2-$bet_commission
			);
			$bet365->log->addlog(print_r($insert,1),'update.log');
			$idrate = CTable('match_rate')->insert($insert);
		}else{
			$infoMatch = file_get_contents('http://www.win29.com/betting.es.php?page=info&matchID='.$match[2]);
			$infoMatch = getInfoMatch($infoMatch);
			$id = $matchAdded[$match[2]];
			
			//-----------------------------
			//http://www.flashscore.com/x/feed/d_od_8QbkWRd8_en_1_eu
			//http://www.flashscore.com/x/feed/d_od_8QbkWRd8_en_1_eu
			
			$bet365 = new Bet('http://www.flashscore.com/x/feed/d_od_'.$match[2].'_en_1_eu');
			if ($bet365->continue == false) continue;
			$odds = $bet365->getBetODDS();
			$ov_ud = $bet365->getBetOverUnder();
			$hadicap = $bet365->getBetHandicap();
			//-----------------------------		
			$update = array(
					'ft_home' => $bet365->realRate($odds['odds']['home'], $bet_commission),
					'ft_draw' =>$bet365->realRate($odds['odds']['draw'], $bet_commission),
					'ft_away' => $bet365->realRate($odds['odds']['away'], $bet_commission),
					'dc_hod' => $infoMatch[8],
					'dc_aod' => $infoMatch[9],
					'dc_hoa' => $infoMatch[10],
					'ah_home' => $bet365->realRate($hadicap['home'], $bet_commission),
					'ah_away' => $bet365->realRate($hadicap['home'], $bet_commission),
					'ah_rate_goal' => $hadicap['rate'],
					'ahs_home' => $infoMatch[21],
					'ahs_away' => $infoMatch[22],
					'ahs_rate_goal' => $infoMatch[20],
					'ou_over' => $bet365->realRate($ov_ud['over'], $bet_commission),
					'ou_rate_goal' => $ov_ud['total'],
					'ou_under' => $bet365->realRate($ov_ud['under'], $bet_commission),
					'ts_even' => 2-$bet_commission,
					'ts_odd' => 2-$bet_commission
			);
			$bet365->log->addlog(print_r($update,1),'update.log');
			$idrate = CTable('match_rate')->update($update,array('match_id'=>$id));
		}
// 		break;
	}
	
print_r($matches);